
/**
 * Created with IntelliJ IDEA.
 * User: vuntermann
 * Date: 20/05/13
 * Time: 18:36
 * To change this template use File | Settings | File Templates.
 */
class CashRegister {

    Map priceList
    Map promotions

    CashRegister(priceList, promotions){
        this.priceList = priceList
        this.promotions = promotions
    }

    def calculateTotalFor(products){
        def ticket = calculateSubtotalOf(products)
        int totalDiscount = ticket.getTotalDiscount(promotions, priceList)
        ticket.total = ticket.subTotal - totalDiscount
        return ticket
    }

    private Ticket calculateSubtotalOf(products) {
        def ticket = new Ticket()
        int subTotal = 0
        products.each {product ->
            ticket.add(product)
            subTotal += priceList[product]
        }
        ticket.subTotal = subTotal
        ticket
    }


}

